// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

/**
 * 帐变记录界面
 */
cc.Class({
    extends: cc.Component,

    properties: {
        recordContent: cc.Node,  //帐变记录中scrollview中content
        labBalance: cc.Label,   //余额
        labCashMoney: cc.Label,  //可提现余额
        recordPrefab: cc.Prefab,  //账单预制
        _selectTg: null,  //单选按钮选中(账变记录单选按钮)
        isNowRefreshing:false,

    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this._curPage = 1;
        this.isNowRefreshing = false;
        this.infoRecord(this._curPage);
    },

    infoRecord:function(page){
        var recv = function(ret){
            ComponentsUtils.unblock();
            if (ret.Code !== 0) {
                cc.error(ret.Msg);
                ComponentsUtils.showTips(ret.Msg);
            }else{
                var arry = ret.Data;
              
                if(arry == 0)  //没有账变记录
                {
                    return;
                }
                this.labCashMoney.string = LotteryUtils.moneytoServer(ret.Withdraw).toFixed(2)+' 元宝';
                this.labBalance.string = LotteryUtils.moneytoServer(ret.Balance).toFixed(2)+' 元宝';

                for(var i=0;i<ret.Data.length;i++)
                {
                    var remarks = ret.Data[i].Remark.split('-');                    
                    if(remarks.length>0)
                    {
                        var time = ret.Data[i].Time;
                        var years = time[0]+time[1]+time[2]+time[3];
                        var months = time[4]+time[5];
                        var days = time[4]+time[5] +"-"+ time[6]+time[7];
                        var ymdtimes = "时间："+years+"-"+days+" "+time[8]+time[9]+ ":" +time[10]+time[11]+":"+time[12]+time[13];
                       
                        var item = cc.instantiate(this.recordPrefab);
                        item.getComponent(item.name).init({
                            whereDce: remarks[0],
                            where:remarks[1],
                            Money:LotteryUtils.moneytoServer(ret.Data[i].Amount),
                            ymdtimes: ymdtimes,
                        });  
                        this.recordContent.addChild(item);   

                    }
                }
                this._curPage++;
            }
            this.isNowRefreshing = false;
        }.bind(this);

        var data = {
            Token: User.getLoginToken(),
            UserCode:  User.getUserCode(),
            PageNumber:page,
            RowsPerPage:16,
        };
        CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.GETDEALRECORDLIST, data, recv.bind(this),"POST");  
        ComponentsUtils.block();
    },

    init: function(para){
        this._selectTg =para;
    },

    resetRefresh:function(){
        this.recordContent.removeAllChildren();
        this._curPage = 1;
        this.isNowRefreshing = false;
        this.infoRecord(this._curPage);
    },

    //滚动获取下一页
    scrollCallBack: function (scrollview, eventType) {
        if(eventType === cc.ScrollView.EventType.BOUNCE_BOTTOM)
        {
            //下拉刷新,当底部的offset抵达到某个点的时候
            var offset_y = scrollview.getScrollOffset().y;
            var max_y = scrollview.getMaxScrollOffset().y; 
            if(offset_y - max_y>200){
                if(this.isNowRefreshing == false){
                    this.isNowRefreshing = true;
                    this.infoRecord(this._curPage);
                } 
            }
        }
    },

    //下拉刷新
    scrollRefresh: function (scrollview, eventType, customEventData) {
        if(eventType === cc.ScrollView.EventType.BOUNCE_TOP)
        {
            //下拉刷新,当底部的offset抵达到某个点的时候
            var offset_y = scrollview.getScrollOffset().y;
            var max_y = scrollview.getMaxScrollOffset().y; 
            if(max_y - offset_y >200){
                this.resetRefresh();
            }
        }
    },

    onClose: function(){
        this._selectTg.isChecked =false;
        this.node.getComponent("Page").backAndRemove();  
    },

    // update (dt) {},
});
